//
//  ALCalendarCell.m
//  ALCalendarPickerDemo
//
//  Created by Arclin on 2017/6/16.
//  Copyright © 2017年 arclin. All rights reserved.
//

#import "ALCalendarCell.h"
#import "ALCalendarDate.h"
#import <Masonry/Masonry.h>
#import <Charts-Swift.h>
#import "NSDate+HYBHelperKit.h"
#import "YSTimeUtil.h"


#define KLightGrayColor [UIColor colorWithRed:127.0/255.0 green:143.0/255.0 blue:164.0/255.0 alpha:1]

#define KDarkGrayColor  [UIColor colorWithRed:44.0/255.0 green:49.0/255.0 blue:53.0/255.0 alpha:1]

#define KGrayColor  [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1]

@interface ALCalendarCell()

@property (nonatomic, strong) UILabel *dateLabel;
///有数据时的小圆点
@property (nonatomic, strong) UILabel *roundDateLabel;
///选中日期的背景label
@property (nonatomic, strong) UILabel *selectedLabel;

@end

@implementation ALCalendarCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]) {
       
        [self setupView];
    }
    return self;
}


- (void)setupView
{
    [self addSubview:self.dateLabel];
    [self.dateLabel addSubview:self.roundDateLabel];
    
    self.layer.cornerRadius = 5.0f;
    
    __weak typeof(self) weakSelf = self;
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
}

- (void)setDate:(ALCalendarDate *)date
{
    _date = date;
    [self setRoundDateLabelFrame:date];
    self.dateLabel.text = date.date;
    
    if (date.notThisMonth) {
        self.selectedLabel.hidden = YES;
        self.roundDateLabel.hidden = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.dateLabel.textColor = [UIColor clearColor];
    } else {
        self.backgroundColor = [UIColor whiteColor];
        
        NSDictionary * dict = [self.glucoseArray firstObject];
        NSArray *dayArray = dict[date.date];
        self.roundDateLabel.hidden = (dayArray.count <= 0);
    
        self.dateLabel.textColor = dayArray.count <= 0 ? KGrayColor :KDarkGrayColor;
        
        //新增
        if (dayArray.count > 0 && date.date.integerValue == self.selectedDay.integerValue) {
            self.selectedLabel.hidden = NO;
            self.dateLabel.textColor = [UIColor whiteColor];
            [self insertSubview:self.selectedLabel atIndex:0];
        }
        else{
            self.selectedLabel.hidden = YES;
        }
    }
}


- (void)setRoundDateLabelFrame:(ALCalendarDate *)date
{
    if ([date.date integerValue] < 10 ) {
        self.roundDateLabel.frame = CGRectMake(2, (SCREEN_WIDTH/20)/2 + 10, 4, 4);
    }
    else{
        self.roundDateLabel.frame = CGRectMake(6, (SCREEN_WIDTH/20)/2 + 10, 4, 4);
    }
}


- (void)setWeekDay:(NSString *)weekDay
{
    self.selectedLabel.hidden = YES;
    self.roundDateLabel.hidden = YES;
    
    _weekDay = weekDay;
    self.dateLabel.text = weekDay;
    self.dateLabel.textColor = KDarkGrayColor;
}

- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.textColor = KDarkGrayColor;
        _dateLabel.font = [UIFont systemFontOfSize:14];
    }
    return _dateLabel;
}


- (UILabel *)roundDateLabel
{
    if (!_roundDateLabel) {
        _roundDateLabel = [[UILabel alloc] init];
        _roundDateLabel.backgroundColor = [UIColor colorWithHexColor:@"#287EFF"];
        _roundDateLabel.layer.cornerRadius = 4/2;
        _roundDateLabel.layer.masksToBounds = YES;
    }
    return _roundDateLabel;
}


- (UILabel *)selectedLabel
{
    if (!_selectedLabel) {
        _selectedLabel = [[UILabel alloc] init];
        _selectedLabel.backgroundColor = [UIColor colorWithHexColor:@"#287EFF"];
        CGFloat x = 0;
        CGFloat y = 0;
        if ([UIScreen mainScreen].bounds.size.width > 375) {
            x = 2;
            y = 2;
        }
        _selectedLabel.frame = CGRectMake(x, y, 34, 34);
        _selectedLabel.layer.cornerRadius = 34/2;
        _selectedLabel.layer.masksToBounds = YES;
    }
    return _selectedLabel;
}
@end
